import { CHANGE_INPUT_VALUE, ADD_TODO_ITEM, DELETE_TODO_ITEM, GET_SEARCH_LIST } from './actionTypes';

const defaultState = {
	inputValue: 'hello world',
	list: []
}

const reducer = (state = defaultState, action) => {
	if (action.type === CHANGE_INPUT_VALUE) {
		const newState = JSON.parse(JSON.stringify(state));
		newState.inputValue = action.value;
		return newState;
	}
	if (action.type === ADD_TODO_ITEM) {
		const newState = JSON.parse(JSON.stringify(state));
		newState.list.push(newState.inputValue);
		newState.inputValue = '';
		return newState;
	}
	if (action.type === DELETE_TODO_ITEM) {
		const newState = JSON.parse(JSON.stringify(state));
		newState.list.splice(action.index, 1);
		return newState;
	}
	if (action.type === GET_SEARCH_LIST) {
		const newState = JSON.parse(JSON.stringify(state));
		action.data.reverse().forEach(item => {
			newState.list.unshift(item);
		});
		return newState;
	}
	return state;
}

export default reducer;